<?php
/**
 * <https://y.st./>
 * Copyright © 2017 Alex Yst <mailto:copyright@y.st>
 * 
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 * 
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
 * GNU General Public License for more details.
 * 
 * You should have received a copy of the GNU General Public License
 * along with this program. If not, see <https://www.gnu.org./licenses/>.
**/

$xhtml = array(
	'<{title}>' => 'Assembly',
	'<{subtitle}>' => 'Written in <span title="Software Engineering 1">CS 2401</span>, finalised on 2017-12-27',
	'<{copyright year}>' => '2017',
	'takedown' => '2017-11-01',
	'<{body}>' => <<<END
<p>
	The first thing I did was look at the table of available calculations and try to figure out how it worked.
	In particular, I was confused by what it meant by &quot;a&quot; equalling <code>0</code> or <code>1</code>.
	After some scanning of the reading material, I found &quot;a&quot; is just the first bit of the compute field.
	The table was just poorly-designed; that bit should&apos;ve just been included alongside the &quot;c*&quot; bits.
	I&apos;m not sure even why this bit is called &quot;a&quot; and not &quot;c0&quot;.
	Next, I needed to look up in this week&apos;s chapter how to use the <code>@</code> symbol.
	I remembered reading about it being used to stash values in some way, but I didn&apos;t remember specifically how to use it.
	It&apos;s used to store values in the A register, meaning it translates into an A-command, not a C-command; everything else is C-commands.
	A-commands are the easiest to translate though, as they just involve converting to base two, padding to fifteen bits, and prepending a zero.
	With all the information I needed in hand, it was simply a matter of going over the program line-by-line.
	If the instruction was an <code>@</code> instruction, I converted, padded, and prepended.
	If it was anything else, I set the first three bits to <code>111</code>, looked up the computation in the provided table, set the next seven bits according to the table, and set the final six bits depending on where the value should be saved and whether any jumping was desired.
	The placement of the jump bits is intuitive, but I needed to take note that the first destination bit saves to the A register, the second to the D register, and the third to the memory location specified by using the value in the A register as a memory address; they&apos;re in alphabetical order.
	Lastly, I worked backwards, translating from machine code to assembly, to make sure I hadn&apos;t made any mistakes.
</p>
<h2>First program</h2>
<h3>Provided assembly code</h3>
<blockquote>
<pre><code>//Program 1 to convert to machine language
// Computes R0 = 2 + 3

@2
D=A
@3
D=D+A
@0
M=D</code></pre>
</blockquote>
<h3>My translation into machine code by hand</h3>
<blockquote>
<pre><code>0000000000000010
1110110000010000
0000000000000011
1110000010010000
0000000000000000
1110001100001000</code></pre>
</blockquote>
<h2>Second program</h2>
<h3>Provided assembly code</h3>
<blockquote>
<pre><code>// Program 2 to convert to machine language
// Symbol-less version of the Max.asm program. 

@0
D=M
@1
D=D-M
@10
D;JGT
@1
D=M
@12
0;JMP
@0
D=M
@2
M=D
@14
0;JMP</code></pre>
</blockquote>
<h3>My translation into machine code by hand</h3>
<blockquote>
<pre><code>0000000000000000
1111110000010000
0000000000000001
1111010011010000
0000000000001010
1110001100000001
0000000000000001
1111110000010000
0000000000001100
1110101010000111
0000000000000000
1111110000010000
0000000000000010
1110001100001000
0000000000001110
1110101010000111</code></pre>
</blockquote>
<h2>What I learned</h2>
<p>
	When I was initially reading about A-commands, I immediately saw a small difficulty that would need to be worked around when programming for the Hack platform.
	Specifically, values cannot be directly entered into the D register, but only the A register.
	This means that values need to be entered into the A register, then <strong>*moved*</strong> to the D register if needed.
	And if you needed to keep the value in the A register?
	You&apos;d need to stash the value from the A register in memory, enter your new value into the A register, move it to the D register, then pull the original value back from memory and put it back in the A register.
	What a mess.
	And now that I think about it, what do you do if you need to choose a memory location dynamically?
	You can&apos;t store the memory location where you have access to it while writing values into the system.
	Lovely.
</p>
<p>
	As I completed this project though, I noticed another small problem.
	Values cannot simply be moved between registers; they must be <strong>*computed*</strong>.
	Moving a value from A to D means having to compute an output value that is equal to unmodified A, then save that output to D.
	I&apos;m not sure how efficient or inefficient that is right now, but it definitely eats a clock cycle.
	Keeping the commands understood by the processor simple provides a lot of power, as you can string together simple commands to create complex ones.
	It does involve making the processor do a bunch of extra, irrelevant work though.
	When writing software on the Hack platform, you also have to be careful about where you save your data.
	I noticed that the first program deliberately stored the result in the D register because the A register was about to be needed to set the memory address to save the final result to.
	And again, a computation and a clock cycle were needed to save the value in the D register to memory.
	Only having two registers on hand is a pain.
	If the platform had three registers, two could&apos;ve held the values to be added together and the third held the memory address.
	The result would never have been needed to be temporarily stored in the D register because it could be saved directly to M.
</p>
<p>
	I also found it interesting that there weren&apos;t separate bits for determining which registers to read from and choosing the operation to perform.
	Instead, you can&apos;t know either of these pieces of information without knowing all seven bits of the compute field.
	Additionally, it appears some bit combinations are unused.
	I wonder what would happen if these bit combinations were used in a program.
	If these operations are undefined, it might be implementation-specific.
	In any case, it&apos;s best to avoid those combinations unless simply experimenting.
	I kind of wish we&apos;d built the Hack computer in class like the book had intended; maybe we&apos;ll do that in a future unit.
</p>
END
);
